package com.hspedu.newData.dmsxl.树.二叉树的遍历.层序遍历迭代法.队列;

import com.hspedu.newData.dmsxl.ds.TreeNode;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-21 
 * @Version: 1.0
 */

public class LC1161最大层内元素和 {

    // todo 只写了bfs的，还需要dfs
    public int maxLevelSum(TreeNode root) {

        Queue<TreeNode> queue = new ArrayDeque<>();

        queue.offer(root);

        int max = -1000000;
        int index = 0;
        int i = 0;
        while (!queue.isEmpty()){

            i++;
            int len = queue.size();
            int sum = 0;
            while (len>0){
                TreeNode node = queue.poll();
                sum += node.val;

                if (node.left!=null) queue.offer(node.left);
                if (node.right!=null) queue.offer(node.right);
                len--;
            }
            if (max<sum){
                max = sum;
                index = i;
            }
        }

        return index;

    }
}
